<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=latin1">
        <link rel="stylesheet" type="text/css" href="../padrao.css" />
        <link rel="stylesheet" href="../jQuery/lista.css" type="text/css" media="all" />
        <script type="text/javascript" src="../jQuery/jquery-1.2.1.pack.js"></script>
        <script type="text/javascript" src="../jQuery/busca.js"></script>
        <script type="text/javascript" src="../sorttable.js"></script>

        <title> Template </title>

        <?php
        ob_start();
        session_start();
        $default_area = "Escritorio";
        //$default_area = "Portaria";
        $_SESSION[default_area] = $default_area;
        include '../infobox.php';
        include '../verifica_usuario.php';
        include '../functions.php';
        include '../functions_office.php';
        include '../menu.php';
        ?>

    </head>
    <body>
        <div class="conteudo">
            <form method="POST" onsubmit="return valida(this);" action="devolver_creditos.php">
                <table>
                    <tr>
                        <td>Digite a empresa:</td>
                        <td><input type="text" size="30" name="empresa_nome" id="inputString" onKeyUp="lookup(this.value);" onBlur="fill();" autocomplete="off"/>
                            <div class="suggestionsBox" id="suggestions" style="display: none;">
                                <img src="../jQuery/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
                                <div class="suggestionList" id="autoSuggestionsList">
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Aterro:</td>
                        <td>
                            <?php
                            include("mysqlconfig.inc");
                            $query = "SELECT nome FROM ATERROS";
                            /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
                            $result = mysql_query($query);
                            echo '<select name="nome"></option>';
                            // printing the list box select command

                            while ($nt = mysql_fetch_array($result)) {//Array or records stored in $nt
                                echo "<option value=$nt[nome]>$nt[nome]</option>";
                                echo "porra";
                                /* Option values are added by looping through the array */
                            }
                            echo "</select>"; // Closing of list box
                            mysql_close($con);
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td>Quantos creditos? </td>
                        <td><input name="cred_devolvido" type="text" autocomplete="off"></td>
                    </tr>
                    <tr>
                        <td>Credito de:</td>
                        <td>
                            <select name="tipo">
                                <option value = Poly > Poly </option>
                                <option value = Truck > Truck </option>
                                <option value = Toco > Toco </option>
                                <option value = Carreta > Carreta </option>
                                <option value = Transbordo > Transbordo </option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td><input value="Calcular" type="submit"></td>
                    </tr>
                </table>
            </form>
            <?php
                            $empresa_nome = $_POST[empresa_nome];
                            if ($empresa_nome != "") {
                                $tipo = $_POST[tipo];
                                $cred_devolvido = $_POST[cred_devolvido];
                                $aterro = $_POST[nome];
                                $total = 0;

                                //VERIFICA SE TEM CREDITOS
                                getCreditos_tipo($tipo, $aterro);
                                $cred_atual = $_SESSION[creditos];
                                $campo = $_SESSION[campo];
                                echo "cred_atual: $cred_atual <br>";

                                if ($cred_devolvido > $cred_atual ) {
                                    //creditos insuficientes
                                    echo "Não tem creditos para serem devolvidos";
                                } else {
                                    echo "cred_devolvido: $cred_devolvido <br>";
                                    echo "cred_atual: $cred_atual <br>";
                                    echo "nome: $empresa_nome <br>";
                                    echo "tipo: $tipo <br>";
                                    echo "aterro: $aterro <br>";


                                    //creditos suficientes
                                    include("mysqlconfig.inc");
                                    $query = "select * from VENDAS
                                      WHERE EMPRESAS_nome = '$empresa_nome'
                                      AND tipo_viagem = '$tipo'
                                      AND ATERROS_nome = '$aterro'
                                      ORDER BY data ASC
                                    ";
                                    $res = mysql_query($query);

                                    $num_reg = mysql_num_rows($res);
                                    for ($i = 0; $i < $num_reg + 1; $i++) {
                                        $mostra = mysql_fetch_array($res);
                                        $viagens = $mostra[viagens];
                                        $custo_unitario = $mostra[custo_unitario];

                                        if ($total < $cred_devolvido) {

                                            $total = $total + ($viagens * $custo_unitario);
                                            $_SESSION[total] = $total;
                                            echo "$total <BR>";
                                        } else{


                                            echo "$total <BR>";
                                            $result2 = mysql_query("UPDATE CREDITOS SET $campo = ($campo - $cred_devolvido)
                                                                   WHERE ATERROS_nome = '$aterro'
                                                                   AND EMPRESAS_nome = '$empresa_nome'");
                                            if (!$result2) {
                                                die('Could not query: UPDATE devolver creditos ' . mysql_error());
                                            }
                                            $num_reg = mysql_affected_rows();
                                            if ($num_reg == 1) {
                                                echo "<br> Sucesso <br>";
                                            } else {
                                                echo "<br> Falhou <br>";
                                            }


                                            break;
                                        }
                                    }
                                }
                            }
            ?>
        </div>
    </body>
</html>
